//
//  UIBarButtonItem+Extension.m
//  OC-Sina
//
//  Created by 陈高健 on 15/12/22.
//  Copyright © 2015年 陈高健. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)

//通过类方法,快速实现barButtonItem的样式
+ (instancetype)itemWithImageName:(NSString *)imageName target:(nullable id)target action:(nullable SEL)action;
{
    //设置Nav左边按钮
    UIButton *button = [[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] forState:UIControlStateHighlighted];
   
    //设置按钮点击事件
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    //设置按钮大小等于当前按钮上图片的大小
    button.size = button.currentImage.size;
    return [[UIBarButtonItem alloc]initWithCustomView:button];
}

+ (instancetype)itemWithImageName:(NSString *)imageName title:(NSString *)title target:(nullable id)target action:(nullable SEL)action{
    //设置Nav左边按钮
    UIButton *button = [[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] forState:UIControlStateHighlighted];
    
    //设置按钮点击事件
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    //设置title的颜色
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor colorWithRed:53/255.0 green:53/255.0 blue:53/255.0 alpha:1] forState:UIControlStateNormal];
    
    //设置title的文字
    [button setTitle:title forState:UIControlStateNormal];
    
    //根据内容自动调整大小
    [button sizeToFit];
    return [[UIBarButtonItem alloc]initWithCustomView:button];
}

@end
